package com.magic.gre.base.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import com.magic.gre.base.dialog.BaseDialog;
import com.magic.gre.helper.Apphelper;
import com.magic.gre.ui.activity.LoginActivity;
import com.magic.gre.ui.dialog.LoginHintDialog;
import com.noname.lib_base_java.entity.MsgEvent;
import com.noname.lib_base_java.net.RxBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.functions.Consumer;

/**
 * Author: Lmoumou
 * Date: 2018-12-11 16:34
 * 文件名: BaseFragment
 * 描述:
 */
public abstract class BaseFragment extends Fragment implements LoginHintDialog.OnLoginHintDialogListener,
        BaseDialog.DismissListanner {
    protected Context mContext;
    //    protected int statusBarCorlor;
//    protected int contentColor;
    Unbinder unbinder;

    private LoginHintDialog loginDialog;

    public LoginHintDialog getLoginDialog() {
        if (loginDialog == null) {
            loginDialog = new LoginHintDialog();
        }
        return loginDialog;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view, savedInstanceState);
    }

    /**
     * 显示软键盘
     */
    public void showSoftInput(View view) {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.showSoftInput(view, 0);
        }
    }

    /**
     * 关闭软键盘
     */
    public void closeKeyBord(View view) {
        ((InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(view.getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
//
    }

    /**
     * 订阅RxBus
     */
    @SuppressLint("CheckResult")
    protected void registerRxBus() {
        RxBus.getInstance().toObservable(this, MsgEvent.class)
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent msgEvent) throws Exception {
                        handlerMsg(msgEvent);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });

    }

    protected void handlerMsg(MsgEvent msgEvent) {

    }


    /**
     * 检测是否登陆
     *
     * @return
     */
    public boolean isValidAccount(){
        if (TextUtils.isEmpty(Apphelper.getToken())){

            if (!getLoginDialog().isResumed()) {
                showLoglinDialog();

            }

            return false;
        }
        return true;
    }

    public void showLoglinDialog() {
        getLoginDialog().setDismissListanner(this);
        getLoginDialog().showThis(getChildFragmentManager(), LoginHintDialog.class.getSimpleName(),this);
    }

    @Override
    public void onDialogMiss() {
        getActivity().finish();
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onLogin() {
        LoginActivity.startThis(mContext);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    /**
     * 布局文件Id
     *
     * @return
     */
    protected abstract int getLayoutId();


    /**
     * 初始化
     *
     * @param view
     * @param savedInstanceState
     */
    protected abstract void initView(View view, Bundle savedInstanceState);
}
